<template>
  <div class="container">
      <h2 class="handle-title">系统管理 > 角色管理</h2>
      <div class="handle-box">
        <div>
          <span class="search-label" style="margin-left: 5px">角色名字:</span>
          <el-input v-model="search" style="width: 120px" placeholder="请输入角色名字"></el-input>
          <span class="search-label" style="margin-left: 5px">角色状态:</span>
          <el-select v-model="value" style="width: 120px">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-button class="query_btn" type="primary" @click="goSearch">查询</el-button>
          <el-button class="add_btn" type="primary" @click="addRole">添加</el-button>
        </div>
      </div>
      <el-table
        :data="tableData"
        class="table"
        border>
        <el-table-column prop="rolename" label="名字" />
        <el-table-column label="状态" >
          <template #default="{row}">
             {{row.rolestate == 1?"可用":"不可用"}}
          </template> 
        </el-table-column>
        <el-table-column prop="roledesc" label="说明" />
        <el-table-column label="操作" width="200">
          <template #default="{row}">
            <el-button type="text" icon="el-icon-edit-outline" @click="goUpdate(row.roleid)">修改</el-button>
            <el-button
              type="text"
              icon="el-icon-unlock"
              class="red"
              @click="goRight(row.roleid)">权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next"
            :total="totalNum">
          </el-pagination>
      </div>
  </div>
</template>

<script>
import { getRoleList } from "../../api/public/api";

export default {
  name: '',
  data() { 
    return {
      value:"-1", //状态选择默认为 -1 表示全部
      options:[
        {
          value: '-1',
          label: '全部'
        },
        {
          value: '1',
          label: '可用'
        },
        {
          value: '0',
          label: '不可用'
        }
      ],
      tableData: [], //数据列表
      currentPage: 1, //默认第一页
      pageSize: 10, //默认每页10条
      totalNum: 0, //默认总条数
      search: "", //搜索名字
    }
  },
  mounted() {
    //获取角色管理数据初始值
    this.roleList()
  },
  methods:{
    //每页条数
    handleSizeChange(val) {
      this.pageSize=val;
      this.roleList()
    },
    // 第几页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.roleList()
    },
    //封装请求方法
    roleList(){
      let params ={
        pagenum:this.currentPage,
        pagesize:this.pageSize,
        // rolename:this.search,
        // rolestate:this.value
      }
      if(this.search){
        params.rolename=this.search
      }

      getRoleList(params).then((res)=>{
        
        // console.log(res)
        this.tableData = res.data.pagedata.records;
        this.totalNum = res.data.pagedata.total;
      })
    },
    //Search搜索按钮
    goSearch(){
      this.roleList()
    },
    //新增角色
    addRole(){
      this.$router.push({
        path:"/demo",
        // params:{
        //   id:1,
        //   name:"jack"

        // }
        query:{

        }
      })
      // this.$router.push("/addRole")
    },
    //修改
    goUpdate(id){
      // console.log(id)
      this.$router.push({
        path:"/updaterole",
        query:{id}
      })
    },
    //权限管理
    goRight(id){
      this.$router.push({
        path:"/right",
        query:{id}
      })
    }
  },
 }
</script>

<style>
.handle-title{
  line-height: 50px;
}
</style>